<?php

namespace App\Http\Repositories\UserManage;

use App\Http\Repositories\Repository;
use App\Models\Role;

/**
 * 角色
 */
class RoleRepo extends Repository
{
    const MODEL = Role::class;

    public function search(array $params)
    {
        $conditions = [];
        if (!empty($params['name'])) {
            array_push($conditions, ['name', 'where', ['like', '%' . $params['name'] . '%']]);
        }

        if (!empty($params['alias'])) {
            array_push($conditions, ['alias', 'where', ['like', '%' . $params['alias'] . '%']]);
        }

        if (!empty($params['begin_time'])) {
            array_push($conditions, ['created_at', 'where', ['>=', $params['begin_time']]]);
        }

        if (!empty($params['end_time'])) {
            array_push($conditions, ['created_at', ' where', ['<=', $params['end_time']]]);
        }

        return $this->formatSearch($conditions);
    }

    public function getByAlias($alias)
    {
        return $this->query()->where('alias', $alias)->first();
    }
    public function getRoleIdByAlias($alias)
    {
        $role = $this->query()->where('alias', $alias)->first();

        return !empty($role) ? $role->id : 0;
    }
}
